package leetcode_题库;

/**
 * Created by IntelliJ IDEA.
 *
 * @ProjectName leetcode
 * @Author Ding
 * @CreateTime 2022/8/29 9:13 星期一
 */
public class _226_翻转二叉树 {
    /**
     * 深优递归即可
     * > 2022/08/29 09:13:27
     * 解答成功:
     * 	执行耗时:0 ms,击败了100.00% 的Java用户
     * 	内存消耗:39 MB,击败了64.20% 的Java用户
     * 	其实本意就是left指向右子结点，right指向左子结点。但是当前者指向完毕后，原本的值会发生改变，所以需要临时结点去等于指向前的n
     * @param root
     * @return
     */
    public TreeNode invertTree(TreeNode root) {
        if (root == null){
            return null;
        }

        //临时结点
        TreeNode n = root.left;
        root.left = invertTree(root.right);
        root.right = invertTree(n);

        return root;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
